'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument
dim h_height:h_height=2
dim l_height:l_height=0.6
dim usrLyrObj
dim sellyr:sellyr = application.gui.activeroutelayer
main
msgbox "done!"
Set usrLyrObj = nothing
set doc = nothing
sub main()	
	on error resume next
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	'// TODO
	dim uh,ul
	uh = "zyq_chk_h"
	ul = "zyq_chk_l"
	call CheckUserlyr(uh)
	call CheckUserlyr(ul)
	scheme = "Sys: zyq_chk_hl"
	doc.ActiveView.DisplayControl.LoadScheme (scheme)
	
	dim numPoints 
	redim pntArr(2,4)
	numPoints = 5 
	dim x1,x2,y1,y2
		
	doc.currentunit=4'mm
	dim userlyr
	dim po,pos
	dim selside:selside = 512
	if sellyr = 1 then selside = 1
	dim cmp,cmps
	set cmps = doc.components(0)
	for each cmp in cmps
		userlyr =""
		if cmp.side = selside then
			set pos = cmp.PlacementOutlines
			for each po in pos	
				if po.height>h_height then userlyr = uh
				if po.height<l_height then userlyr = ul
				if userlyr <>"" then
					Set usrLyrObj = doc.FindUserLayer(userlyr)
					x1 =po.Extrema.minx 
					y1 =po.Extrema.miny 
					X2 =po.Extrema.maxx 
					Y2 =po.Extrema.maxy 
					pntArr(0,0)=x1:pntArr(1,0)=y1:pntArr(2,0)=0
					pntArr(0,1)=x2:pntArr(1,1)=y1:pntArr(2,1)=0
					pntArr(0,2)=x2:pntArr(1,2)=y2:pntArr(2,2)=0
					pntArr(0,3)=x1:pntArr(1,3)=y2:pntArr(2,3)=0
					pntArr(0,4)=x1:pntArr(1,4)=y1:pntArr(2,4)=0
					call doc.PutUserLayerGfx(usrLyrObj,0.1,numPoints,pntArr,true,Nothing,4)	
				end if										
			next		
		end if			
	next	
end sub

